# -- coding: utf-8 --

import urllib
import urllib2
import json
import os
import time
import pymysql
import pymysql.cursors

connection = pymysql.connect(host='localhost',
                             user='www_jc8mo4_cn',
                             db='www_jc8mo4_cn',
                             password='3HE3sdAcCjA4kthp',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor);

def checkDomain(domain):
    mystatus = 0
    requrl = "http://lj.52jcc.cc/wxjc.php"
    formate = {
        'type':11,
        'key': 's6FRNbpKRCtEC3LG21QtP3qOV0TCyr8tvPzgSA',
        'ym': domain
    }
    try:
        time.sleep(0.1)
        data = urllib.urlencode(formate)
        # proxy_support = urllib2.ProxyHandler({"http":"http://127.0.0.1:1080"})
        # opener = urllib2.build_opener(proxy_support)
        # urllib2.install_opener(opener)
        request = urllib2.Request(requrl, data)
        request.add_header('Referer','http://lj.52jcc.cc/wx.php?key=s6FRNbpKRCtEC3LG21QtP3qOV0TCyr8tvPzgSA')
        request.add_header('X-Requested-With','XMLHttpRequest')
        #request.add_header('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36')
        #request.add_header('Referer','http://lj.52jcc.cc/wx.php?key=c9ygGQdNOr8vSyLFeXnGJ3EsghDlRxJkJC7I8N')
        #request.add_header('Connection','keep-alive')
        #request.add_header('Content-Type','application/x-www-form-urlencoded; charset=UTF-8')

        response = urllib2.urlopen(request,timeout=3)
        js = json.loads(response.read())
        print(js)
        mystatus = js['code']['status']
        print(domain+" status:%s" % mystatus)
        if int(mystatus) == 2:
            print("222222222")
            return True
        elif int(mystatus) == 4:
            print("44444444444")
            print(domain+" is unkown,retry -------------------------------------------------")
            return checkDomain(domain)
        elif int(mystatus) == 3:
            print("33333333333333")
            return False
        else:
            print("xxxxxxxxxxxxxxxx")
            return False
    except Exception as e:
        return checkDomain(domain)


with connection.cursor() as cursor:
    # 忽略检测超过3次仍然被封的域名
    sql = "select * from fa_domain where status!=3 limit 600,200"
    cursor.execute(sql)
    rs = cursor.fetchall()
    for (index,domainObj) in enumerate(rs):
        id = domainObj.get('id')
        domain = domainObj.get('domain')
        status = domainObj.get('status')
        update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print("-------------------%s-------------------" % domain)
        if checkDomain(domain):
            # 域名正常
            status = 0
            print(domain+" is ok")
        else:
            print(domain+" is broken")
            status = status+1
        update_sql = "update fa_domain set status=%d,update_time='%s' where id=%d" % (status,update_time,id)
        rt = cursor.execute(update_sql)
        connection.commit()
        print("-------------------%s-------------------" % domain)
        print("")

connection.close()
